package com.example.demo.leetcode.top100;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _57电话号码的字母组合
 * @description
 * @date 2023/06/20 17:27
 * <p>
 * ******************************************************
 */
public class _57电话号码的字母组合 {

    List<String> result = new LinkedList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return result;
        }

        StringBuilder trace = new StringBuilder();
        backtrack(0, digits, trace);

        return result;
    }

    private void backtrack(int i, String digits, StringBuilder trace) {
        if (trace.length() == digits.length()) {
            result.add(trace.toString());
        }

        if (i >= digits.length()) {
            return;
        }
        char[] stringByChar = getStringByChar(digits.charAt(i));
        for (int j = 0; j < stringByChar.length; j++) {
            char c = stringByChar[j];
            trace.append(c);
            backtrack(i + 1, digits, trace);
            trace.deleteCharAt(trace.length() - 1);

        }
    }


    private char[] getStringByChar(char c) {
        String res;
        switch (c) {
            case '2':
                res = "abc";
                break;
            case '3':
                res = "def";
                break;
            case '4':
                res = "ghi";
                break;
            case '5':
                res = "jkl";
                break;
            case '6':
                res = "mno";
                break;
            case '7':
                res = "pqrs";
                break;
            case '8':
                res = "tuv";
                break;
            case '9':
                res = "wxyz";
                break;
            default:
                res = "";
                break;
        }
        return res.toCharArray();
    }
}
